package com.sds.dao;

import java.sql.*;
import java.util.ArrayList;

import com.sds.util.Util_DB;
import com.sds.vo.ReserveVO;

public class ReserveDAO {

	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;

	public void insertReserve(ReserveVO Reserve) {
		try {
			conn = Util_DB.connection();
			if (conn != null) {
				pstmt = conn
						.prepareStatement("insert into t_Reserve values(?, ?)");
				pstmt.setInt(1, Reserve.getRes_code());
				pstmt.setString(2, Reserve.getRes_mem_id());
				pstmt.setString(3, Reserve.getRes_line_id());
				pstmt.setString(4, Reserve.getRes_sitno());
				pstmt.setDate(5, Reserve.getRes_time());

				int result = pstmt.executeUpdate();

				if (result == 1)
					System.out.println("Success Insert Reserve");
				else
					System.out.println("Falled Insert Reserve");
			} else {
				System.out.println("DB Connection Error");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			Util_DB.disconnection(null, pstmt, conn);
		}
	}

	public ReserveVO selectReserve(ReserveVO Reserve) {
		ReserveVO ReserveVO = null;
		try {
			conn = Util_DB.connection();
			if (conn != null) {
				pstmt = conn
						.prepareStatement("select * from t_Reserve where res_code = ?");
				pstmt.setInt(1, Reserve.getRes_code());
				rs = pstmt.executeQuery();
				if (rs.next()) {
					ReserveVO = new ReserveVO(rs.getInt(1), rs.getString(2),
							rs.getString(3), rs.getString(4), rs.getDate(5));
				}
			} else {
				System.out.println("DB Connection Error");
			}
			return ReserveVO;
		} catch (SQLException e) {
			e.printStackTrace();
			return ReserveVO;
		} finally {
			Util_DB.disconnection(null, pstmt, conn);
		}
	}

	public ArrayList<ReserveVO> selectReserveList() {

		ArrayList<ReserveVO> ReserveList = new ArrayList<ReserveVO>();
		;
		try {
			conn = Util_DB.connection();
			if (conn != null) {
				pstmt = conn.prepareStatement("select * from t_Reserve");
				ResultSet rs = pstmt.executeQuery();

				while (rs.next()) {
					ReserveList.add(new ReserveVO(rs.getInt(1),
							rs.getString(2), rs.getString(3), rs.getString(4),
							rs.getDate(5)));
				}
				return ReserveList;
			} else {
				System.out.println("DB Connection Error");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			Util_DB.disconnection(rs, pstmt, conn);
		}
		return ReserveList;
	}

	public void updateReserve(ReserveVO Reserve) {

		try {
			conn = Util_DB.connection();
			if (conn != null) {
				pstmt = conn
						.prepareStatement("update t_Reserve set res_mem_id = ?, res_line_id = ?, res_sitno, res_time where Reserve_id = ?");

				pstmt.setString(1, Reserve.getRes_mem_id());
				pstmt.setString(2, Reserve.getRes_line_id());
				pstmt.setString(3, Reserve.getRes_sitno());
				pstmt.setDate(4, Reserve.getRes_time());
				pstmt.setInt(5, Reserve.getRes_code());

				pstmt.executeUpdate();
			} else {
				System.out.println("DB Connection Error");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			Util_DB.disconnection(null, pstmt, conn);
		}
	}

	public void deleteReserve(ReserveVO Reserve) {

		try {
			conn = Util_DB.connection();
			if (conn != null) {
				pstmt = conn
						.prepareStatement("delete t_Reserve where Reserve_id = ?");
				pstmt.setInt(1, Reserve.getRes_code());
				pstmt.executeUpdate();
			}
			else{
				System.out.println("DB Connection Error");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			Util_DB.disconnection(null, pstmt, conn);
		}
	}
}